package com.hdl;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class PartitionerDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 1、获取配置信息， 或者job对象实例
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, "PartitionerDriver");

        // 2、指定本程序的jar包所在的本地路径
        job.setJarByClass(PartitionerDriver.class);

        // 3、指定业务job要使用的mapper/reducer
        job.setMapperClass(SortMapper.class);
        job.setReducerClass(SortReducer.class);

        // 4、指定mapper输出数据的kv类型
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(LongWritable.class);

        // 5、指定最终的输出的数据的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //指定reduceTask的数量，默认是1个
        job.setNumReduceTasks(1);

        // 6、指定job的输出原始文件夹所在目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7、将job中配置的相关参数 以及job所有的java类所在的jar包，提交给yarn去运行
        boolean flag = job.waitForCompletion(true);
        System.exit(flag ? 0 : 1);
    }
}
